//
//  String.swift
//  WBCloud
//
//  Created by 李加建 on 2021/9/7.
//

import Foundation

public extension String {
    var urlQueryString: String? {
        addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    }

    var unUrlQueryString: String? {
        removingPercentEncoding
    }
}

public extension String {
    var intValue: Int? {
        Int(self)
    }
    
    var floatValue: Float? {
        Float(self)
    }
    
    var doubleValue: Double? {
        Double(self)
    }
}

public extension String {
    /// 截取字符串前 offset 位
    /// - Parameter offsetBy: 位数
    /// - Returns: 截取后的字符串
    subscript(cut offsetBy: Int) -> String {
        if count > offsetBy {
            let indexStart = startIndex
            let indexEnd = index(indexStart, offsetBy: offsetBy)
            let string = String(self[indexStart ..< indexEnd])
            return string
        }
        return self
    }
}
